# coding=utf8
import os
import sys
import time
import unittest
import HTMLTestRunner
import logging
from utili import LogConfigYaml
# 根目录
path = os.path.dirname(os.path.abspath(__file__))
# 把路径添加到环境变量
sys.path.append(path)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))

print(u"接口测试开始 start")
print(os.path.join(path, "outFiles/reports", time.strftime("%Y%m%d")))
re_dir = os.path.join(path, "outFiles/reports", time.strftime("%Y%m%d"))
print(re_dir+"-------------------------报告路径")
if not os.path.exists(re_dir):
    os.mkdir(re_dir)
path = path+'/testCases'

# 加载测试case
data = unittest.defaultTestLoader.discover(path, pattern='*.py')
print(data)

if __name__ == '__main__':
    LogConfigYaml.setup_logging()
    logger = logging.getLogger('server')  # 维护一个全局日志对象
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    filename = os.path.join(re_dir, now + '_result.html')
    fp = open(filename, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,    title="接口自动化测试报告",        description="用例执行结果")
    runner.run(data)
    fp.close()
    print(u"\n接口测试结束 end ")